/**
 * 浏览器BOM工具类
 */
// 获取URL查询参数值
export function getQuery(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}

// 序列化URL查询参数
export function qs(data) {
  let res = ''
  for (const key in data) {
    res += encodeURIComponent(key) + '=' + encodeURIComponent(data[key]) + '&'
  }
  if (res.length > 0) {
    res = '?' + res.substring(0, res.length - 1)
  }
  return res
}

// 打开Url
export function openUrl(url, type = '_blank') {
  window.open(url, type)
}

// 下载Url
export function downloadUrl(url) {
  window.open(url, '_blank')
  // const downloadLink = document.createElement('a')
  // downloadLink.download = filename
  // downloadLink.style.display = 'none'
  // downloadLink.href = url
  // document.body.appendChild(downloadLink)
  // downloadLink.click()
  // document.body.removeChild(downloadLink)
}

// 下载文件
export function downloadFile(obj, suffix = 'xlsx', name = 'download') {
  const url = window.URL.createObjectURL(new Blob([obj]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  const fileName = name + '.' + suffix
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
